/*
* yasl-qt (or simply yasl) is a qt-based program for symbian devices
* and intended to manage a shopping list.
* The acronym yasl stands for Yet Another Shopping List.
* 
* Copyright (C) 2011 - Alessandro Orlandi
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "AdaptiveKeyboard.h"
#include "Key.h"
#include <QGridLayout>
#include <QPushButton>

AdaptiveKeyboard::AdaptiveKeyboard(QWidget *parent) :
    QFrame(parent)
{
    mVisible = false;
    hide();

    mCol = 5;
    mButtonWidth = 61;
    mButtonHeigth = 41;

    mGridLayout = new QGridLayout(this);
    mGridLayout->setContentsMargins(0,0,0,0);
    QRect lFrameGeometry = geometry();    
    setGeometry(lFrameGeometry.x(), lFrameGeometry.y(), width(), 6  * ( mButtonHeigth+ 5 ));
}

void AdaptiveKeyboard::notifyKeyPressed(QString pText)
{
    emit sigKeyPressed(pText);
}

bool AdaptiveKeyboard::toggleVisible()
{
    if (mVisible)
    {
        hide();
    }else
    {
        show();
        sigKeyPressed("");
    }
    mVisible = ! mVisible;
    return true;
}

void AdaptiveKeyboard::updateKeyboard(QStringList pItemsList, QString pTextFilter)
{

    mKeysList.clear();

    for (int i=0; i < pItemsList.length(); i++)
    {
        QString lItem = pItemsList.at(i);
        if (pTextFilter.compare("") != 0)
        {
            for (int lPos = lItem.indexOf(pTextFilter,0,Qt::CaseInsensitive);
                 lPos > -1 && lPos+1 < lItem.length();
                 lPos = lItem.indexOf(pTextFilter,lPos+1,Qt::CaseInsensitive)
                 )
            {
                if (lPos + pTextFilter.length() < lItem.length())
                {
                    mKeysList.append(lItem.at(lPos + pTextFilter.length()).toLower());
                }
            }
        }
        else
        {
            for(int i=0; i< lItem.length(); i++)
            {
                QString lLabel( lItem.at(i).toLower() );
                mKeysList.append(lLabel);
            }
        }
    }
    mKeysList.removeDuplicates();
    mKeysList.sort();
    mKeysList << "<<";
    redrawKeyboard();

}
void AdaptiveKeyboard::redrawKeyboard()
{
    //clear the previous keyboard
    for (int i=0; i < mKeys.length(); i ++ )
    {
        mGridLayout->removeWidget(mKeys.at(i));
        delete mKeys.at(i);
    }

    mKeys.clear();

    //calculate mRow
    //increments mRow by one until mRow * mCol is greater than or equal to mKeys length
    for (mRow = 1; mRow * mCol < mKeysList.length() ; mRow++ );


    QRect lFrameGeometry = geometry();
    int lNewHeight = mRow  * ( mButtonHeigth+5 );
    int lNewY = lFrameGeometry.y() + ( height() - lNewHeight );

    setGeometry(lFrameGeometry.x(),  lNewY , width() , lNewHeight);

    int i = mKeysList.length()-1;
    for (int r = mRow -1 ; ((r >= 0 ) && (i >= 0)); r--)
    {        
        mGridLayout->setRowStretch(r,1);

        for (int c = mCol -1; ((c >= 0 ) && (i >= 0)) ; c--)
        {
            mGridLayout->setColumnMinimumWidth(c, mButtonWidth);
            mGridLayout->setColumnStretch(c,1);

            Key *lTmpButton = new Key();
            lTmpButton->setText(mKeysList.at(i).toStdString().c_str());
            lTmpButton->setFixedSize(mButtonWidth, mButtonHeigth);
            connect(lTmpButton,SIGNAL(pressed(QString)),this,SLOT(notifyKeyPressed(QString)));
            mKeys.append(lTmpButton);
            mGridLayout->addWidget(lTmpButton, r, c);
            i--;
        }
    }


}

void AdaptiveKeyboard::reset()
{
    mVisible = false;
    hide();
    emit sigKeyboardReset();
}
